package zhtt01.mysql.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import zhtt01.mysql.entity.Organization;

import java.util.List;

@Repository
@Transactional
public interface OrganizationRepository extends JpaRepository<Organization, Long> {

    @Modifying
    @Query(value = "update organization set name = :name where id = :id",nativeQuery = true)
    void updateNameById(@Param("id") Long id, @Param("name") String name);

    @Query(value = "select * from springboot_mysql_organization where parent_id = :pid",nativeQuery = true)
    List<Organization> getOrgByPId(@Param("pid")Long pid);

    @Query(value = "select * from springboot_mysql_organization where parent_id = :pid and id != :id",nativeQuery = true)
    List<Organization> getOrgByPId(@Param("pid")Long pid,@Param("id")Long id);

    @Query(value = "select * from springboot_mysql_organization where parent_id is null",nativeQuery = true)
    List<Organization> getRootOrganizationList();

    @Query(value = "select * from springboot_mysql_organization where parent_id is null and id != :id",nativeQuery = true)
    List<Organization> getRootOrganizationList(@Param("id")Long id);

}